package cn.xm.test;

import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.mail.SimpleMailMessage;
import org.springframework.mail.javamail.JavaMailSender;
import org.springframework.mail.javamail.MimeMessageHelper;
import org.springframework.test.context.junit4.SpringRunner;

import javax.mail.internet.MimeMessage;
import java.io.File;

@RunWith(SpringRunner.class)
@SpringBootTest
public class Emailtest {

    // 邮件核心对象
    @Autowired
    private JavaMailSender javaMailSender;

    @Test
    public void test1() throws Exception{
        // 设置要发送的邮件
        SimpleMailMessage simpleMailMessage = new SimpleMailMessage();
        // 设置发件人
        simpleMailMessage.setFrom("775366739@qq.com");
        // 设置收件人
        simpleMailMessage.setTo("775366739@qq.com");
        // 设置标题
        simpleMailMessage.setSubject("测试发送邮件的标题");
        // 设置正文
        simpleMailMessage.setText("测试发送邮件的正文");
        // 使用邮件核心对象发送邮件
        javaMailSender.send(simpleMailMessage);
    }

    @Test
    public void sendMail() throws Exception{
        // 1.创建复杂邮件对象
        MimeMessage mimeMessage = javaMailSender.createMimeMessage();
        // 2.发送复杂邮件的工具类
        MimeMessageHelper helper = new MimeMessageHelper(mimeMessage,true,"utf-8");
        helper.setFrom("775366739@qq.com");
        helper.setSubject("测试复杂邮件");
        // 3.设置文本内容，第二个参数 true 代表是否是一个html内容
        helper.setText("<h1>男德经</h1>"+
                "<img src='http://dfs.java.itsource.cn/group1/M00/00/05/oYYBAGKDLDuADv2ZAABvwky9b5015.jpeg\"' />",true);
        // 4.添加附件
        helper.addAttachment("好东西.jpg",new File("D:\\download\\学习资料\\41460946_1084624958364654_2132497699650404352_n.jpg"));
        // 5.设置收件人
        helper.setTo("775366739@qq.com");
        // 6.发送邮件
        javaMailSender.send(mimeMessage);
    }
}
